Object Oriented Programming (OOP) in C++
As the name suggests, it brings the use of "objects" in programming. Basically, they are some guidelines for running efficient programs.
Let me explain the difference between tradition style and OOP style of programming with an example:
Traditional: running(); // we don't know who's running.
OOP: Jack.running(); // here we know who's running. Jack is the object here.
The main aim of OOP is to bind together the data and functions that work on them, so that no other part of the code can access the data, except that function.
CLASS
class Car
{
// These are some of the properties that every Car share in common.
int number_of_wheels;
int cost;
bool fuel_type;
int increase_speed()
{ /* some code */ }
};
OBJECTS
class Car
{
// These are some of the properties that every Car share in common.
int number_of_wheels;
int cost;
bool fuel_type;
int increase_speed()
{ /* some code */ }
};
int main()
{
Car c1; // object of car class initialised.
}
ENCAPSULATION
ABSTRACTION
Encapsulation vs Abstraction:
Example:
class Customer
{
public:
string customer_name = "";
string customer_id = "";
void Add()
{
validate();
Add_to_database();
}
private:
void validate()
{ /* some code */ }
void Add_to_database()
{ /* some code */ }
};
How is Abstraction done ?
pow()
in our code that come under math.h
. We don't know the underlying algorithm used (hidden), but we just use it in our codes.POLYMORPHISM
Types:
class C
{
void fun(int x)
{...}
void fun(double y)
{...}
void fun(int , int)
{...}
};
b. Operator Overloading
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
// This is automatically called when '+' is used with
// between two Complex objects
// Operator overloading part.
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
INHERITANCE
class Animal
{
public:
void eat()
{
cout<<"Eating..."<< endl;
}
};
class Dog: public Animal
{
public:
void bark()
{
cout<<"Barking...";
}
};
class Base
{
public:
int fun() { cout << "Base::fun() called"; }
int fun(int i) { cout << "Base::fun(int i) called"; }
};
class Derived: public Base
{
public:
int fun(char x) { cout << "Derived::fun(char ) called"; }
};
int main()
{
Derived d;
d.fun();
return 0;
}
The above code gives error upon compilation, as fun() of base class is not accessible in the derived class.
Modes of Inheritance
Types of Inheritance in C++
ACCESS MODIFIER
Important Points
Friend Class
class Node {
private:
int key;
Node* next;
/* Other members of Node Class */
// Now class LinkedList can
// access private members of Node
friend class LinkedList;
};
Another Example:
#include
using namespace std;
class A
{
int x;
public:
A()
{
x=10;
}
friend class B; //friend class
};
class B
{
public:
void display(A &t)
{
cout<
#include
using namespace std;
class Point {
private:
int x, y;
public:
Point(){
x = 0;
y = 0;
}
Point(int x1, int y1)
{
x = x1;
y = y1;
}
// Copy constructor
Point(const Point& p1)
{
x = p1.x;
y = p1.y;
}
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p0 = Point();
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
// Let us access values assigned by constructors
cout << "p0.x = " << p0.getX()
<< ", p0.y = " << p0.getY();
cout << "\np1.x = " << p1.getX()
<< ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX()
<< ", p2.y = " << p2.getY();
return 0;
}
DESTRUCTOR
~
before) which destroys or deletes an object. [~ constructor_name]
VIRTUAL FUNCTION
What if We Do not define virual for an overring method?
So with base class pointer dervieved class object the method of derived class will not be overridden. It calls the base class, if we do not use virtual
keyword.
Example:
#include
using namespace std;
class A {
public:
virtual void fun() { cout << "\n A::fun() called "; } //note 1
};
class B : public A {
public:
void fun() { cout << "\n B::fun() called "; }
};
class C : public B {
public:
void fun() { cout << "\n C::fun() called "; }
};
int main()
{
// An object of class C
C c;
// A pointer of class B pointing
// to memory location of c
B* b = &c;
// this line prints "B::fun() called" if note1 is not virtual
// otherwise it prints "C::fun() called"
b->fun();
getchar(); // to get the next character
return 0;
}
ABSTRACT CLASS
PURE VIRTUAL FUNCTION
class X
{
public:
virtual void show() = 0; // pure virtual func
};